/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.gzchengguo.cn
* 注意：
* 本软件为www.gzchengguo.cn开发研制，未经购买不得商用
* 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
* 一经发现盗用、分享等行为，将追究法律责任，后果自负
*/
package link.chengguo.orangemall.modules.activity.service.impl;

import cn.hutool.core.util.ObjectUtil;
import link.chengguo.orangemall.common.service.impl.BaseServiceImpl;
import link.chengguo.orangemall.dozer.service.IGenerator;
import link.chengguo.orangemall.modules.activity.domain.OrangeStoreBargainUser;
import link.chengguo.orangemall.modules.activity.domain.OrangeStoreBargainUserHelp;
import link.chengguo.orangemall.modules.activity.service.OrangeStoreBargainUserHelpService;
import link.chengguo.orangemall.modules.activity.service.OrangeStoreBargainUserService;
import link.chengguo.orangemall.modules.activity.service.mapper.OrangeStoreBargainUserHelpMapper;
import link.chengguo.orangemall.modules.activity.vo.OrangeStoreBargainUserHelpQueryVo;
import link.chengguo.orangemall.modules.user.domain.OrangeUser;
import link.chengguo.orangemall.modules.user.service.OrangeUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;


/**
 * <p>
 * 砍价用户帮助表 服务实现类
 * </p>
 *
 * @author chengguo
 * @since 2019-12-21
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class OrangeStoreBargainUserHelpServiceImpl extends BaseServiceImpl<OrangeStoreBargainUserHelpMapper, OrangeStoreBargainUserHelp> implements OrangeStoreBargainUserHelpService {

    @Autowired
    private IGenerator generator;

    @Autowired
    private OrangeStoreBargainUserHelpMapper orangeStoreBargainUserHelpMapper;

    @Autowired
    private OrangeStoreBargainUserService storeBargainUserService;
    @Autowired
    private OrangeUserService userService;




    /**
     * 获取砍价帮
     * @param bargainId 砍价商品id
     * @param bargainUserUid 砍价用户id
     * @param page page
     * @param limit limit
     * @return list
     */
    @Override
    public List<OrangeStoreBargainUserHelpQueryVo> getList(Long bargainId, Long bargainUserUid,
                                                           int page, int limit) {
        OrangeStoreBargainUser storeBargainUser = storeBargainUserService
                .getBargainUserInfo(bargainId,bargainUserUid);
        if(ObjectUtil.isNull(storeBargainUser)) {
            return Collections.emptyList();
        }
        Page<OrangeStoreBargainUserHelp> pageModel = new Page<>(page, limit);
        QueryWrapper<OrangeStoreBargainUserHelp> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(OrangeStoreBargainUserHelp::getBargainUserId,storeBargainUser.getId())
                .orderByDesc(OrangeStoreBargainUserHelp::getId);
        List<OrangeStoreBargainUserHelpQueryVo> storeBargainUserHelpQueryVos = generator
                .convert(orangeStoreBargainUserHelpMapper.selectPage(pageModel,wrapper).getRecords(),
                        OrangeStoreBargainUserHelpQueryVo.class);

        storeBargainUserHelpQueryVos.forEach(item->{
            OrangeUser orangeUser = userService.getById(item.getUid());
            item.setAvatar(orangeUser.getAvatar());
            item.setNickname(orangeUser.getNickname());
        });

        return storeBargainUserHelpQueryVos;
    }

    /**
     * 获取砍价帮总人数
     * @param bargainId 砍价产品ID
     * @param bargainUserUid 用户参与砍价表id
     * @return int
     */
    @Override
    public int getBargainUserHelpPeopleCount(Long bargainId, Long bargainUserUid) {
        return this.lambdaQuery()
                .eq(OrangeStoreBargainUserHelp::getBargainUserId,bargainUserUid)
                .eq(OrangeStoreBargainUserHelp::getBargainId,bargainId)
                .count();
    }





}
